<?php
namespace App\Model;
use App\Helpers\Wx;
use Illuminate\Database\Eloquent\Model;

class WeixinShare extends Model
{

    public function ticket($url)
    {
        //获取相关数据star
        $wx = new WX(env('WX_APP_ID'), env('WX_APP_SECRET'));
        $access_token = $wx->getToken();
        $str = $this->curl_https("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" . $access_token . "&type=jsapi");
        $str = json_decode($str,true);
        $share['ticket'] = $str['ticket'];
        $share['timestamp'] = time();
        $share['noncestr'] = str_shuffle('abcdefghijklmnopqretuvwxyz');
        $share['url'] = "http://".$url;
        $share['signature'] =  sha1("jsapi_ticket=".$share['ticket']."&noncestr=".$share['noncestr']."&timestamp=".$share['timestamp']."&url=".$share['url']);

        return $share;
    }

    public function curl_https($url)
    {
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }

    }
}